<!--头部公共部分-->
{include file="common/header" /}

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>设置</a>
        <a>
          <cite>教师管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="wodekehu" class="x-body" class="clear:both;">
    <div class="layui-row"
         style="padding-left:20px;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane"  lay-filter="form-organization">
            <div class="layui-input-inline">
                <input type="text" name="teacher_name" placeholder="请输入教师名称" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <select name="status">
                    <option value="">请选状态</option>
                    <option value="1">启用中</option>
                    <option value="0">禁用中</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" id="search">
                    <i class="layui-icon layui-icon-search"></i>筛选</button>
                <button class="layui-btn layui-btn-danger" id="clear">
                    <i class="layui-icon"></i>重置</button>
                <button class="layui-btn" id="table-add">
                    <i class="layui-icon layui-icon-add-circle-fine"></i>新增教师</button>
                <button type="button" class="layui-btn layui-btn-normal layui-btn-radius" id="btn"><i class="layui-icon layui-icon-upload"></i>导入Excel</button>
                <a href="/admin/model/teacher[老师模板].xlsx" class="layui-btn layui-btn-radius layui-btn-normal" id="upload_teacher"><i class="layui-icon layui-icon-download-circle"></i>下载老师模板</a>
            </div>
        </div>
    </div>
    <table class="layui-hide" id="LAY_table_user" lay-filter="table"></table>
    <script type="text/html" id="barTable">
        {{# if(d.status == 0){ }}
        <a class="layui-btn layui-btn-xs" style="background-color: #009688" lay-event="check">启用</a>{{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="check">禁用</a>{{# } }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>
    layui.use(['form', 'element','table', 'layer', 'laytpl', 'upload'], function () {
        var $ = layui.$,
            form = layui.form,
            table = layui.table,
            element = layui.element,
            upload = layui.upload,
            layer = layui.layer;

        var loading = layer.load();
        let type = "{$type}";
        $('#table-add').click(function () {
            if(type == 1){
                layer.open({
                    type: 2,
                    skin: 'layui-layer-lan',
                    closeBtn: 2,
                    title: '选择集团',
                    area: ['50%', '60%'],
                    shadeClose: false,
                    content: "{:url('Teacher/company')}",
                });
            }else{
                let company_id = "{$company_id}";
                let url = "{:url('Teacher/teacher_add')}?company_id="+company_id;
                layer.open({
                    type: 2,
                    title: '新增教师',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['70%', '60%'],
                    content: url
                });
            }
        });

        //文件上传
        var isLoad;
        var uploadInst = upload.render({
            elem: '#btn',
            url: '/admin/Teacher/teacher_excel',
            before: function(obj) {
                isLoad = layer.load(0, {
                    shade: false
                });
            },
            done: function(res) {
                //如果上传失败
                if(res.code > 0) {
                    return layer.msg('导入失败');
                }
                //上传成功
                layer.msg(res.name+'导入成功');
                layer.close(isLoad);
                location.reload();
            },
            accept: 'file' ,//允许上传的文件类型
            //,size: 50 //最大允许上传的文件大小
            error: function() {
                    return layer.msg('导入失败');
            }
        });


        //数据表格 方法级渲染
        var tableIns = table.render({
            elem: '#LAY_table_user'
            , url: '{:url("Teacher/lists")}'
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {field: 'id', title: '教师编号', width: 100, sort: true, fixed: false, align: 'center'
                }, {field: 'teacher_name', title: '教师名称', width: 100, align: 'center',edit: 'text'
                }, {field: 'campus_id', title: '所属校区', width: 100, align: 'center'
                }, {field: 'teacher_department', title: '部门', width: 200, align: 'center'
                }, {field: 'teacher_subject', title: '科目', width:200, align: 'center'
                }, {field: 'teacher_grade', title: '年级', width: 100, align: 'center'
                }, {field: 'phone', title: '电话', width: 200, align: 'center'
                }, {field: 'status_text', title: '状态', width: 100, align: 'center'
                }, {field: 'create_time', title: '创建时间', width: 200, align: 'center'
                }, {field: 'update_time', title: '最新时间', width: 200, align: 'center'
                },  {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barTable'}
            ]]
            , height: 'full-180'
            , page: true
            , done: function () {
                $("[data-field='id']").css('display','none');
                layer.close(loading);
            }
        });

        table.on('edit(table)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            if(field === 'teacher_name'){
                $.post('{:url("Teacher/teacher_edit")}'
                    ,{
                        'id':data.id,
                        'teacher_name':value
                    }
                    ,function(result){
                        if(result.code === 1){
                            location.reload();
                        }
                    });
            }
        });
        //监听工具条
        table.on('tool(table)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') { //删除
                layer.confirm('真的删除么', function (index) {
                    $.post("{:url('Teacher/teacher_del')}", {
                        id: data.id
                    }, function (result) {
                        if (result.code === 1) {
                            layer.msg(result.msg, {
                                time: 2000
                            });
                            setTimeout(function () {
                                obj.del(); //删除对应行（tr）的DOM结构
                                location.reload();
                            }, 2000)
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }, "json")
                });
            }else if (layEvent === 'check') {
                var status;
                if (data.status === 0) {
                    status = 1;
                } else {
                    status = 0;
                }
                //激活与禁用状态改变
                $.post("{:url('Teacher/teacher_status')}", {
                    id: data.id,
                    status: status
                }, function (rJson) {
                    if (rJson.code === 1) {
                        layer.msg(rJson.msg, {
                            time: 2000
                        });
                        setTimeout(function () {
                            location.reload();
                        }, 2000)
                    } else {
                        layer.msg(rJson.msg, {
                            icon: 2,
                            time: 2000
                        });
                    }
                }, "json")
            }
        });
        //搜索项
        $('#search').click(function () {
            var field = {};
            field.status = $('select[name=status] option:selected').val();
            field.teacher_name = $('input[name=teacher_name]').val();
            console.log(field);
            tableIns.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });//清除
        $('#clear').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns.reload({
                where: {
                    'teacher_name': '',
                    'status': '',
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

    });
</script>